/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.meanzoft.m3d.font;

import org.meanzoft.m3d.resource.ResourceManager;
import static org.lwjgl.opengl.GL11.*;

/**
 *
 * @author Meanz
 */
public class TextureMappedFont {

    private int base;
    private String fontFile;

    public TextureMappedFont(String fontFile) {
        this.fontFile = fontFile;
        ResourceManager.getManager().loadTexture(fontFile + "_font_0", fontFile);
        glPushMatrix();
        buildFont();
        glPopMatrix();
    }

    private void buildFont() {                                  // Build Our Font Display List
        float cx;                                             // Holds Our X Character Coord
        float cy;                                             // Holds Our Y Character Coord

        base = glGenLists(256);                            // Creating 256 Display Lists
        ResourceManager.getManager().getTexture(fontFile + "_font_0").bind();     // Select Our Font Texture
        for (int i = 0; i < 256; i++) {                               // Loop Through All 256 Lists
            cx = ((float) (i % 16)) / 16.0f;                     // X Position Of Current Character
            cy = ((float) (i / 16)) / 16.0f;                     // Y Position Of Current Character

            glNewList(base + i, GL_COMPILE);          // Start Building A List
            glBegin(GL_QUADS);                        // Use A Quad For Each Character
            glTexCoord2f(cx, 1 - cy - 0.0625f);            // Texture Coord (Bottom Left)
            glVertex2i(0, 0);                              // Vertex Coord (Bottom Left)
            glTexCoord2f(cx + 0.0625f, 1 - cy - 0.0625f);  // Texture Coord (Bottom Right)
            glVertex2i(16, 0);                              // Vertex Coord (Bottom Right)
            glTexCoord2f(cx + 0.0625f, 1 - cy);            // Texture Coord (Top Right)
            glVertex2i(16, 16);                             // Vertex Coord (Top Right)
            glTexCoord2f(cx, 1 - cy);                      // Texture Coord (Top Left)
            glVertex2i(0, 16);                             // Vertex Coord (Top Left)
            glEnd();                                       // Done Building Our Quad (Character)
            glTranslatef(10.0f, 0.0f, 0.0f);               // Move To The Right Of The Character
            glEndList();                                   // Done Building The Display List
        }                                                       // Loop Until All 256 Are Built
    }

    public void glPrint(int x, int y, String msg, int set) {   // Where The Printing Happens
        if (set > 1) {
            set = 1;
        }
        ResourceManager.getManager().getTexture(fontFile + "_font_0").bind();
        glDisable(GL_DEPTH_TEST);                           // Disables Depth Testing
        glMatrixMode(GL_PROJECTION);                        // Select The Projection Matrix
        glPushMatrix();                                     // Store The Projection Matrix
        glLoadIdentity();                                   // Reset The Projection Matrix
        glOrtho(0, 800, 0, 640, -1, 1);                          // Set Up An Ortho Screen
        glMatrixMode(GL_MODELVIEW);                         // Select The Modelview Matrix
        glPushMatrix();                                     // Store The Modelview Matrix
        glLoadIdentity();                                   // Reset The Modelview Matrix
        glTranslatef(x, y, 0);                                // Position The Text (0,0 - Bottom Left)
        int baseOffset = base + (128 * set);                // Choose The Font Set (0 or 1)
        for (int i = 0; i < msg.length(); i++) {
            glCallList(baseOffset + msg.charAt(i));
            glTranslatef(1.0f, 0.0f, 0.0f);
        }
        glMatrixMode(GL_PROJECTION);                        // Select The Projection Matrix
        glPopMatrix();                                      // Restore The Old Projection Matrix
        glMatrixMode(GL_MODELVIEW);                         // Select The Modelview Matrix
        glPopMatrix();                                      // Restore The Old Projection Matrix
        glEnable(GL_DEPTH_TEST);                            // Enables Depth Testing
    }
}
